package exampleQuestions;

public class MaxProfit {

	/**
	 * given a series of stock prices in an array. Pick when to buy and when to
	 * sell to gain max profit.
	 */
	public static void main(String[] args) {

		int array[] = new int[] { 9, 2, 5, 6, 6, 1, 3, 7 };

		System.out.println(getMaxProfit(array));

	}

	private static int getMaxProfit(int[] array) {

		int maxProfit = 0;
		int lowestPrice = array[0];
		int buyIndex = 0;
		int sellIndex = 0;

		for (int i = 1; i < array.length; i++) {

			if (lowestPrice > array[i]) {
				lowestPrice = array[i];
				buyIndex = i;
			} else if (maxProfit < array[i] - lowestPrice) {
				sellIndex = i;
				maxProfit = array[i] - lowestPrice;
			}
		}
		
		System.out.println(buyIndex + " " + sellIndex);

		return maxProfit;
	}

}
